#include<iostream>

using namespace std;
const long long SIZE = 15;
const long long A_NUM = 10;

long long pow_ll(long long b, long long r)
{
    long long res = 1;
    for(long long i = 0;i<r;++i) res*=b;
    return res;
}

// caller保证传入的进制合法
// 但是可能转换出的结果是会溢出的
long long convert2ten(char* s,long long r)
{
    long long res = 0;
    long long top = 0; while(s[top])top++;
    long long size = top-1;
    while(top>0)
    {
        char tmpc = s[--top];
        res += (isalpha(tmpc) ? tmpc-'a'+A_NUM : tmpc-'0') * pow_ll(r,size-top) ;
    }
    return res;
}

int main(int argc, char const *argv[])
{
//     freopen("test.txt","r",stdin);
    char N1[SIZE]{0},N2[SIZE]{0};
    scanf("%s %s",N1,N2);
    long long tag,radix; cin>>tag>>radix;

    char *r_num = N1, *n_num=N2; if(tag==2)swap(N1,N2);
    long long target = convert2ten(r_num,radix);

    // need to use bisearch!
    long long l=1;
    for(int i=0;n_num[i];++i)
    {
        long long tmp = isalpha(n_num[i]) ? n_num[i]-'a'+A_NUM : n_num[i]-'0';
        l = l<tmp ? tmp : l;
    }
    l+=1;
    long long r = max(target,l);
    long long res = (l+r)/2; 

    while(l<=r)
    {
        res = (l+r)/2;
        long long tmp = convert2ten(n_num,res);
        // 溢出的处理!
        if(tmp<0 || tmp>target) r = res-1;
        else if(tmp<target) l = res+1;
        else break;
    }

    if(convert2ten(n_num,res) == target)
        cout<<res;
    else cout<<"Impossible";

	return 0;
}